package org.grill.fatwhacker.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

import org.joda.time.LocalDate;


public class DailyRecord implements Iterable<FoodEntry>, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ArrayList<FoodEntry> entries = new ArrayList<FoodEntry>();
	
	private LocalDate date;

	public LocalDate getDate() {
		return date;
	}

	public void setDate(LocalDate date) {
		this.date = date;
	}

	public ArrayList<FoodEntry> getEntries() {
		return entries;
	}

	public void setEntries(ArrayList<FoodEntry> entries) {
		this.entries = entries;
	}

	public float getCumTotal() {
		float total = 0;
		for (FoodEntry entry : entries) {
			if (entry.isFood())
				total += entry.getValue();
			else total -= entry.getValue();
		}
		
//		System.out.println("Total: " + total);
		return total;
	}

	public float getExercisePoints() {
		float total = 0;
		for (FoodEntry entry : entries) {
			if (!entry.isFood())
				total += entry.getValue();
		}
		return total;
	}
	
	public float getFlexpointsUsed() {
		float cumTotal = getCumTotal();
		int dailyPoints = week.getDailyPoints();
		if (cumTotal > dailyPoints)
			return (cumTotal - dailyPoints);
		
		return 0;
	}
	

	private FoodWeek week;
	
	public void setWeek(FoodWeek week)
	{
		this.week = week;
	}

	public Iterator<FoodEntry> iterator() {
		return entries.iterator();
	}

	public void dump() {
		System.out.println("Dumping " + date);
		for (FoodEntry entry : this) {
			System.out.println("\t" + entry.toString());
		}
		System.out.println("Total points: " + getCumTotal());
		
	}

	public float getRemaining() {
		float cum = getCumTotal();
		if (cum >= week.getDailyPoints())
			return 0;
		return week.getDailyPoints() - cum;
	}

	public float getFoodTotal() {
		float total = 0;
		for (FoodEntry entry: this) {
			if (!entry.isFood()) continue;
			total += entry.getValue();
		}
		
		return total;
	}
}
